//
//  ImportantNewsVC.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ImportantNewsVC.h"

@interface ImportantNewsVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation ImportantNewsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    // 普通cell
    [_tableView registerClass:[ImportNewsCell class] forCellReuseIdentifier:@"cell"];
    // 要闻三张图片cell
    [_tableView registerClass:[ImportThreePicCell class] forCellReuseIdentifier:@"importThreecell"];
    // 大图片cell
    [_tableView registerClass:[HaveBigPicCell class] forCellReuseIdentifier:@"haveBigPiccell"];
    // 数据处理
    [self data];
    // 刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [self.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header = header;
    // 加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;

}
// 刷新
- (void)headerData
{
    NSString *str =  @"http://c.3g.163.com/nc/article/list/T1429173762551/0-20.html";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"T1429173762551"];
        self.dataArr = [ImportNewsModel modelHandleWithArray:arr];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];

}
// 加载
- (void)footerData
{
    static NSInteger t = 20;
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1429173762551/%ld-20.html", t];
    [AFHTTPHandler GETWithURL:str completion:^(id result) {
        NSArray *arr = [ImportNewsModel modelHandleWithArray:result[@"T1429173762551"]];
        for (NSInteger i = 0; i < arr.count; i++) {
            [_dataArr addObject:arr[i]];
        }
        [self.tableView reloadData];
    }];
    t += 20;
}
// 数据处理
- (void)data
{
    NSString *str =  @"http://c.3g.163.com/nc/article/list/T1429173762551/0-20.html";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"T1429173762551"];
        self.dataArr = [ImportNewsModel modelHandleWithArray:arr];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImportNewsModel *m = _dataArr[indexPath.row];
    // 第一种cell
    if (indexPath.row == 0) {
        HaveBigPicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"haveBigPiccell" forIndexPath:indexPath];
        [cell.imgsrc sd_setImageWithURL:[NSURL URLWithString:m.imgsrc]];
        cell.title.text = m.title;
        cell.digest.text = m.digest;
        cell.replyCount.text = [[m.replyCount stringValue] stringByAppendingString:@"跟帖"];
        return cell;
    }else if (m.imgextra.count == 0) {
        ImportNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.imgsrc sd_setImageWithURL:[NSURL URLWithString:m.imgsrc]];
        cell.title.text = m.title;
        cell.digest.text = m.digest;
        cell.replyCount.text = [[m.replyCount stringValue] stringByAppendingString:@"跟帖"];
        return cell;
    }else{
        ImportThreePicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"importThreecell" forIndexPath:indexPath];
        cell.title.text  =m.title;
        // 取imgextra数组中key为imgsrc对应的值
        // [cell.imgsrcOne sd_setImageWithURL:[NSURL URLWithString:m.imgextra[0] [@"imgsrc"]]];
        [cell.imgsrc sd_setImageWithURL:[NSURL URLWithString:m.imgsrc]];
        [cell.imgsrcOne sd_setImageWithURL:[NSURL URLWithString:m.imgextra[0]]];
        [cell.imgsrcTwo sd_setImageWithURL:[NSURL URLWithString:m.imgextra[1]]];
        cell.replyCount.text = [[m.replyCount stringValue] stringByAppendingString:@"跟帖"];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImportNewsModel *m = _dataArr[indexPath.row];
    if (indexPath.row == 0) {
        return HEIGHT * 300;
    }else if (m.imgextra.count == 0){
        return HEIGHT * 100;
    }else{
        return HEIGHT * 135;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 要闻详情页
    ImportNewsModel *m = _dataArr[indexPath.row];
    ImportantSelectVC *importSelect = [[ImportantSelectVC alloc] init];
    // 传递网址
    importSelect.str = m.url;
    [self.navigationController pushViewController:importSelect animated:YES];
    [importSelect release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
